package com.unc.allergyex;

import java.util.ArrayList;

import android.app.ExpandableListActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.TextView;

import com.unc.allergyex.adapters.DishAdapter;
import com.unc.allergyex.helpers.ObjManager;
import com.unc.allergyex.objects.db.Category;
import com.unc.allergyex.objects.db.Dish;
import com.unc.allergyex.objects.db.Restaurant;

/**This is the activity for the results screen.  It is an expandable list view.
 * It uses the DishAdapter as the adapter, along with the resultsitem.xml and 
 * result.xml layout files.  It displays the menu of a single restaurant, and 
 * displays whether the user can eat each dish.  
 * @author Stephanie
 *
 */
public class Results extends ExpandableListActivity {

	private DishAdapter adapter;
	
	/**This is the onCreate method that is called automatically when this activity is launched.
	 * Sets up the layout elements: sets the header text to be the restaurant name, sets up the 
	 * disambiguate adapter using the categories and dishes from the restaurant (through ObjManager).
	 * @see android.app.Activity#onCreate(android.os.Bundle)
	 * @see com.unc.allergyex.adapters.object.DisambiguateAdapter
	 */
	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.results);
        
        Restaurant r = ObjManager.getRestaurant();
        ArrayList<Category> c = r.getCategories();
        
        ArrayList<String> categories = new ArrayList<String>();
        ArrayList<ArrayList<Dish>> items = new ArrayList<ArrayList<Dish>>();
        
        for (Category cat : c){
        	categories.add(cat.getName()); 
        	items.add(cat.getDishes());
        }
        
        TextView tv = (TextView) findViewById(R.id.restaurantName);
        tv.setText(r.getName());
        
        adapter = new DishAdapter( this, categories, items );
		setListAdapter(adapter);
		
		final Context context = this;

		final ExpandableListView list = getExpandableListView();
		list.setOnChildClickListener(new OnChildClickListener(){
			@Override
			public boolean onChildClick(ExpandableListView parent, View view, int categoryIndex, int dishIndex, long id) {
				Dish item = (Dish) adapter.getChild(categoryIndex, dishIndex);
				Intent intent = new Intent(context, DishBreakdown.class);
				intent.putExtra("dishId", item.getId());
				startActivity(intent);
				return false;
			}
		});
    }
	
	/**
	 * Causes this activity to close.  This method is called when the back button
	 * is tapped.
	 * @param v default parameter for this method
	 */
	public void returnButtonClicked(View v){
		this.finish();
	}
}
